package com.yoo.ons.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import com.yoo.module_base.utils.setOnClickCallback
import com.yoo.ons.R

/**
 * 信息弹窗底部的三个按钮：unlike、like、flashchat
 */
class ProfileInfoBottomMenuDialog private constructor(context: Context, theme: Int) :
    Dialog(context, theme) {

    interface Callback {
        fun onBackClick(type: Int)
    }

    private class Params {
        lateinit var context: Context
        var hasShadow = false
        var canCancel = false
        var callback: Callback? = null
        var openType: Int = 0
    }

    class Builder(context: Context) {
        private val p = Params()

        init {
            p.context = context
        }

        fun setCallback(callback: Callback, type: Int): Builder {
            p.callback = callback
            p.openType = type
            return this
        }

        @SuppressLint("ClickableViewAccessibility")
        fun create(): ProfileInfoBottomMenuDialog {
            val dialog = ProfileInfoBottomMenuDialog(
                p.context,
                if (p.hasShadow) com.yoo.module_res.R.style.Theme_Light_NoTitle_Dialog else com.yoo.module_res.R.style.Theme_Light_NoTitle_NoShadow_Dialog
            )

            val window = dialog.window
            if (window != null) {
                window.setWindowAnimations(com.yoo.module_res.R.style.Animation_Bottom_Rising)
                window.decorView.setPadding(0, 0, 0, 0)
                val lp = window.attributes
                lp.width = WindowManager.LayoutParams.MATCH_PARENT
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT
                window.attributes = lp
                window.setGravity(Gravity.BOTTOM)
            }

            @SuppressLint("InflateParams") val view = LayoutInflater.from(p.context)
                .inflate(R.layout.layout_index_btn, null)

            initialViews(view, dialog)

            dialog.window?.setFlags(
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            )
            dialog.window?.setFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            )
            dialog.setContentView(view)
            dialog.setCanceledOnTouchOutside(p.canCancel)
            dialog.setCancelable(p.canCancel)
            return dialog
        }

        fun show() {
            create().show()
        }

        @SuppressLint("ClickableViewAccessibility")
        private fun initialViews(view: View, dialog: ProfileInfoBottomMenuDialog) {

            val imgProfileInfoUnlike = view.findViewById<ImageView>(R.id.imgProfileInfoUnlike)
            val imgProfileInfoLike = view.findViewById<ImageView>(R.id.imgProfileInfoLike)
            val imgProfileInfoFlash = view.findViewById<ImageView>(R.id.imgProfileInfoFlash)
            val vProfileMenuBottomBg = view.findViewById<View>(R.id.vProfileMenuBottomBg)
            when (p.openType) {
                1 -> {
                    //wlm
                    imgProfileInfoLike.setImageResource(com.yoo.module_res.R.mipmap.img_profile_like)
                    imgProfileInfoUnlike.visibility = View.VISIBLE
                    imgProfileInfoLike.visibility = View.VISIBLE
                    imgProfileInfoFlash.visibility = View.GONE
                    imgProfileInfoUnlike.setImageResource(com.yoo.module_res.R.mipmap.img_card_other_unlike)
                }
                2 -> {
                    //i like
                    imgProfileInfoUnlike.visibility = View.GONE
                    imgProfileInfoLike.visibility = View.GONE
                    imgProfileInfoFlash.visibility = View.VISIBLE
                    imgProfileInfoFlash.setImageResource(com.yoo.module_res.R.mipmap.img_profile_ilike_fc)
                }
                3 -> {
                    //im
                    imgProfileInfoUnlike.visibility = View.GONE
                    imgProfileInfoLike.visibility = View.GONE
                    imgProfileInfoFlash.visibility = View.GONE
                    vProfileMenuBottomBg.visibility = View.GONE
                }
            }

            imgProfileInfoUnlike.setOnClickCallback {
                //点击不喜欢
                p.callback?.onBackClick(1)
            }
            imgProfileInfoLike.setOnClickCallback {
                //喜欢
                p.callback?.onBackClick(2)
            }
            imgProfileInfoFlash.setOnClickCallback {
                //flashchat
                p.callback?.onBackClick(3)
            }
        }
    }
}